import json

def checkVar(func):
    def real(s, uid, pwd):
        if isinstance(uid, str) and isinstance(pwd, str):
            return func(s, uid, pwd)
        else:
            print('Invalid Param.')
    return real

class User:
    def __init__(self):
        self.fp_user = open('users.db', 'r+', encoding='utf-8')
        users = json.loads(str(self.fp_user.read()))
        self.users = users

    @checkVar
    def check(self, uid, pwd='check'):
        return uid in self.users

    @checkVar
    def register(self, uid, pwd):
        if uid in self.users:
            return False
        else:
            self.users[uid.strip()] = pwd.strip()
            self.fp_user.seek(0)
            self.fp_user.write(json.dumps(self.users))
            self.fp_user.flush()
            return True
    
    @checkVar
    def login(self, uid, pwd):
        if uid in self.users and self.users[uid] == pwd:
            return True
        else:
            return False